package com.txsj.scan.presenter;

import com.greendao.GoodsInfoDao;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.GoodsClassify;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.db.GreenDaoUtils;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.view.ISelectGoodsListView;

import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Created by WangChuangJun on 2018/11/22
 * @Describe
 */
public class SelectGoodsListPresenter extends BasePresenter<ISelectGoodsListView> {
    public SelectGoodsListPresenter(ISelectGoodsListView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }


    /**
     * 获取商品分类列表
     * @param type 根据类型加载数据 1:当前仓库，2：其他仓库
     */
    public void getGoodsTypeHttp(String type,String warehouse_id) {
        Map<String,String> params = RetrofitManager.getParams();
        if (type.equals("1")){
            params.put("warehouse_id", warehouse_id);
        }
        view.onLoading();
        if (type.equals("1")) {
            RetrofitManager.getDefault().create(ApiServer.class).getWarehouseGoodsClassify(RetrofitManager.getHeader(), params)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<BaseListResult<GoodsClassify>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            compositeDisposable.add(d);
                        }

                        @Override
                        public void onNext(BaseListResult<GoodsClassify> result) {
                            if (result.isSuccess()) {
                                if (null != result.getData() && result.getData().size() > 0) {
                                    view.onClassifySuccess(result.getData());
                                } else {
                                    view.onError("暂无商品分类");
                                }
                            } else {
                                view.onError(result.getMsg());
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            view.onError("网络异常");
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }else {
            RetrofitManager.getDefault().create(ApiServer.class).getGoodsType(RetrofitManager.getHeader(), params)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<BaseListResult<GoodsClassify>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            compositeDisposable.add(d);
                        }

                        @Override
                        public void onNext(BaseListResult<GoodsClassify> result) {
                            if (result.isSuccess()) {
                                if (null != result.getData() && result.getData().size() > 0) {
                                    view.onClassifySuccess(result.getData());
                                } else {
                                    view.onError("暂无商品分类");
                                }
                            } else {
                                view.onError(result.getMsg());
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            view.onError("网络异常");
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    /**
     * 通过类型和warehouseid筛选
     * @param type
     * @param warehouse_id
     */
    public void getGoodsFilter(String type,int warehouse_id,int goods_class_id){
        if (type.equals("1")){
            List<GoodsInfo> currentGoodsInfoList = GreenDaoUtils.getSingleTon().getmDaoSession().getGoodsInfoDao().queryBuilder()
                    .where(GoodsInfoDao.Properties.Warehouse_id.eq(warehouse_id),GoodsInfoDao.Properties.Goods_class_id.eq(goods_class_id))
                    .list();
            view.onGoodsInfoListSuccess(currentGoodsInfoList);
        }else {
            List<GoodsInfo> otherGoodsInfoList = GreenDaoUtils.getSingleTon().getmDaoSession().getGoodsInfoDao().queryBuilder()
                    .where(GoodsInfoDao.Properties.Warehouse_id.notEq(warehouse_id),GoodsInfoDao.Properties.Goods_class_id.eq(goods_class_id))
                    .list();
            view.onGoodsInfoListSuccess(otherGoodsInfoList);
        }
    }
}
